﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GoogleReaderAccess
{
    //Delegates
    public delegate void LoginExecuted(object sender, LoginEventArgs e);
    public delegate void SubscriptionFetchFailed(object sender, SubscriptionsEventArgs e);
    public delegate void SubscriptionReady(object sender, SubscriptionListData data);

    public class GoogleReader
    {
        
        public GoogleReader(string userName, string password)
        {
            session = new SessionServices(userName, password);
            session.LoginDone += new LoginExecutedInternal(HandleLoginEvent);
            subscriptions = new SubscriptionList(session);
            subscriptions.SubscriptionFailed += new SubscriptionFetchFailedInternal(HandleSubscriptionFailedEvent);
            subscriptions.SubscriptionReady += new SubscriptionReadyInternal(HandleSubscriptionReadyEvent);
        }

        public void login()
        {
            session.doLogin();
        }

        public void getSubscriptionList()
        {
            subscriptions.getSubscriptionList();
        }

        private void HandleLoginEvent(object sender, LoginEventArgs e)
        {
            logged_in = e.Ok;
            if (LoginDone != null)
                LoginDone(this, e);
        }

        private void HandleSubscriptionFailedEvent(object sender, SubscriptionsEventArgs e)
        {
            if (SubscriptionFailed != null)
                SubscriptionFailed(this, new SubscriptionsEventArgs(e.Message));
        }

        private void HandleSubscriptionReadyEvent(object sender, SubscriptionListData data)
        {
            if (SubscriptionReady != null)
            {
                SubscriptionReady(this, data);
            }
        }

        public event LoginExecuted LoginDone;
        public event SubscriptionFetchFailed SubscriptionFailed;
        public event SubscriptionReady SubscriptionReady;

        private SessionServices session;
        private SubscriptionList subscriptions;
        private bool logged_in;
    }
}
